package cn.laixueit.utils;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class EmailUtils {

    public static void main(String[] args) {
        sendEmail("937220832@qq.com");
        /*int i  = (int) (Math.random()*100000);
        System.out.println(i);*/
    }

    public static String sendEmail(String email){
        Properties properties = new Properties();
        // 邮件协议
        properties.setProperty("mail.host","smtp.163.com");

        Session session = Session.getInstance(properties);
        MimeMessage mimeMessage = new MimeMessage(session);

        try {
            mimeMessage.setFrom(new InternetAddress("15618816293@163.com"));
            mimeMessage.setSubject("XX系统激活邮件");

//            mimeMessage.setText("<font style='color:red'>简单内容</font>");

            MimeMultipart multipart = new MimeMultipart();

            MimeBodyPart bodyPart = new MimeBodyPart();
            multipart.addBodyPart(bodyPart);

            String uuid = Utils.getUUID(6);

            String content = String.format("您的激活码为<font style='color:red'>%s</font>", uuid);
            bodyPart.setContent(content,"text/html;charset=utf-8");

            mimeMessage.setContent(multipart);

            mimeMessage.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(email));

            Transport transport = session.getTransport();
            transport.connect("smtp.163.com","15618816293@163.com","WOYPWTAXHOHEFMTZ");
            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
            return uuid;
        }catch (Exception e){
            return null;
        }

    }
}
